<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="query"
        inline size="mini"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-form-item label="角色名称:">
          <el-input
              clearable
              v-model="query.name"
              placeholder="请输入角色名称"/>
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              @click="reload">查询
          </el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="toolbar-box" style="margin-bottom:4px; display: flex; justify-content: flex-end">
        <div class="tool-btn">
          <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              class="ele-btn-icon"
              :disabled="!selection.length"
              @click="removeBatch">删除
          </el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)">添加
          </el-button>
        </div>
      </div>
      <!-- 数据表格 -->
      <el-table
        border
        lazy
        ref="table"
        :data="data"
        v-loading="loading"
        height="calc(100vh - 315px)"
         @selection-change="handleSelectionChange"
      >
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
        <el-table-column label="序号" width="55" align="center" type="index">
          <template slot-scope="scope">
            <span
              v-text="$setTableIndex(query.page, query.limit, scope.$index)"
            >
            </span>
          </template>
        </el-table-column>
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="角色名称" align="center" prop="name" />
        <el-table-column label="备注" align="center" prop="note" />
        <el-table-column label="排序" align="center" prop="sort" />
        <el-table-column label="创建时间" align="center" prop="create_time">
           <template slot-scope="scope">{{$util.toDateString(scope.row.create_time)}}</template>
        </el-table-column>
        <el-table-column
        label="操作"
        align="center"
        fixed="right"
        :resizable="false"
      >
      <template slot-scope="{row}">
          <el-button
            type="text"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            >修改
          </el-button>
          <el-button
            type="text"
            :underline="false"
            icon="el-icon-finished"
            @click="openAuth(row)"
            >分配权限
          </el-button>
          <el-popconfirm
            class="ele-action"
            title="删除角色后，绑定该角色的账号将没有任何权限"
            @confirm="remove(row)">
            <el-button
              type="text"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              >删除
            </el-button>
          </el-popconfirm>
        </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :page.sync="query.page"
        :limit.sync="query.limit"
        @pagination="getData"
      />
    </el-card>
    <!-- 编辑弹窗 -->
    <role-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
    <!-- 权限分配弹窗 -->
    <role-auth
      :data="current"
      :visible.sync="showAuth"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import RoleEdit from './role-edit';
import RoleAuth from './role-auth';

export default {
  name: 'SystemRole',
  components: {RoleEdit, RoleAuth},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      loading: false, // 表格加载状态
      selection: [], // 表格选中数据
      total: 0, // 数据总数
      data: [], // 表格数据接口
      // 表格搜索条件
      query: {
        page: 1,
        limit: 10
      },
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showAuth: false
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    /* 获取表格数据 */
    getData() {
      this.loading = true;
      this.$http
        .get("/role/index", { params: this.query })
        .then(res => {
          this.loading = false;
          this.total = res.data.count;
          this.data = res.data.data;
        })
        .catch(e => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
     /* 查询数据 */
     reload() {
      this.getData();
    },
    /* 重置搜索 */
    reset() {
      this.query = {
        page: 1,
        limit: 10
      };
      this.reload();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection;
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 显示分配权限 */
    openAuth(row) {
      this.current = row;
      this.showAuth = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/role/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的角色吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/role/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status
      })
      this.$http.post('/role/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
        } else {
          row.status = !row.status ? 1 : 0;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
</style>
